package com.park.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.park.web.base.baseOperation.BaseService;
import com.park.web.dao.basSystemDataDictMapper;
import com.park.web.model.domain.basSystemDataDict;
import com.park.web.model.dto.basSystemDataDictDto;
import com.park.web.model.vo.basSystemDataDictVo;
import com.park.web.service.basSystemDataDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class basSystemDataDictServiceImpl extends BaseService<basSystemDataDict> implements basSystemDataDictService {

    @Autowired
    basSystemDataDictMapper ibasSystemDataDictMapper;

    public List<basSystemDataDictVo> selectAllList() {
        var actionList = ibasSystemDataDictMapper.selectAll();
        return this.getTree(actionList, null);
    }

    private List<basSystemDataDictVo> getTree(List<basSystemDataDict> list, String parentModuleKey) {
        List<basSystemDataDict> listChildrens = null;
        if (parentModuleKey == null) {
            listChildrens = list.stream().filter(x -> x.getParentModelKey() == parentModuleKey).collect(Collectors.toList());

        } else {
            list.removeIf(x -> x.getParentModelKey() == null);
            listChildrens = list.stream().filter(x -> x.getParentModelKey().equals(parentModuleKey)).collect(Collectors.toList());
        }
        List<basSystemDataDictVo> listVo = new ArrayList<basSystemDataDictVo>();
        for (var children : listChildrens) {
            basSystemDataDictVo entity = new basSystemDataDictVo();
            entity.setId(children.getId());
            entity.setFullName(children.getModelName());
            boolean hasChildred = parentModuleKey == null ? true : list.stream().filter(x -> x.getParentModelKey().equals(children.getModelKey())).count() > 0;
            entity.setHasChildren(hasChildred);
            entity.setExpanded(parentModuleKey == null ? true : false);
            entity.setDownChecked(false);
            entity.setModel(children);
            entity.setDataType(null);
            entity.setImageUrl(null);
            entity.setChecked(false);
            entity.setDataCode(children.getModelKey());
            var tempList = getTree(list, children.getModelKey());
            if (tempList.size() > 0)
                entity.setItems(tempList);
            listVo.add(entity);
        }
        return listVo;
    }

    public PageInfo<basSystemDataDict> queryWithPage(basSystemDataDictDto entity) {
        PageHelper.startPage(entity.getPage(), entity.getPageSize());
        var actionList = ibasSystemDataDictMapper.queryWithPage(entity);
        return new PageInfo<>(actionList);
    }

    public Integer deleteByIds(String guids) {
        return ibasSystemDataDictMapper.deleteByIds(guids);
    }

    public Map<String, List<basSystemDataDict>> findDataDictListByParentKey(String parentModelKey) {
        var actionList = ibasSystemDataDictMapper.findDataDictListByParentKey(parentModelKey);
        Map<String, List<basSystemDataDict>> mapList = new HashMap<>();
        mapList.put(parentModelKey,actionList);
        actionList.forEach(x -> {
            var tempList = actionList.stream().filter(t -> t.getParentModelKey().equals(x.getModelKey())).collect(Collectors.toList());
            if (tempList.size() > 0)
                mapList.put(x.getModelKey(), tempList);
        });
        return mapList;
    }
}